package com.zhangchuchu.neohealth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangchuchu.neohealth.common.R;
import com.zhangchuchu.neohealth.entity.PointsRecords;
import com.zhangchuchu.neohealth.service.PointsRecordsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (PointsRecords)控制层
 *
 * @author makejava
 * @since 2025-04-16 12:32:28
 */
@RestController
@RequestMapping("/pointsRecords")
public class PointsRecordsController {
    @Resource
    private PointsRecordsService pointsRecordsService;

    /**
     * 查询所有数据（不分页）
     */
    @GetMapping("/getList")
    public R<List<PointsRecords>> selectList() {
        LambdaQueryWrapper<PointsRecords> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(PointsRecords::getCreateTime); // 根据 create_time 降序
        return R.success(pointsRecordsService.list(wrapper));
    }

    /**
     * 分页查询
     */
    @GetMapping
    public R<Page<PointsRecords>> selectAll(Page<PointsRecords> page, PointsRecords pointsRecords) {
        return R.success(pointsRecordsService.page(page, new QueryWrapper<>(pointsRecords)));
    }

    /**
     * 通过主键查询单条数据
     */
    @GetMapping("{id}")
    public R<PointsRecords> selectOne(@PathVariable Serializable id) {
        return R.success(pointsRecordsService.getById(id));
    }

    /**
     * 新增数据
     */
    @PostMapping
    public R<Boolean> insert(@RequestBody PointsRecords pointsRecords) {
        System.out.println("接收到的请求数据：" + pointsRecords);
        return R.success(pointsRecordsService.save(pointsRecords));
    }

    /**
     * 修改数据
     */
    @PutMapping
    public R<Boolean> update(@RequestBody PointsRecords pointsRecords) {
        return R.success(pointsRecordsService.updateById(pointsRecords));
    }

    /**
     * 删除数据（批量）
     */
    @DeleteMapping
    public R<Boolean> delete(@RequestParam("idList") List<Integer> idList) {
        return R.success(pointsRecordsService.removeByIds(idList));
    }

}
